package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.*;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Transactional
    @Override
    public void saveSetmealAndSetmealDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        this.save(setmeal);

        Long dishId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            SetmealDish setmealDish1 = new SetmealDish();
            setmealDish1.setSetmealId(dishId);
            setmealDish1.setDishId(setmealDish.getDishId());
            setmealDish1.setCopies(setmealDish.getCopies());
            setmealDish1.setName(setmealDish.getName());
            setmealDish1.setPrice(setmealDish.getPrice());
            setmealDishService.save(setmealDish1);
        }

    }

    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> voPage = new Page<>();
        Page<Setmeal> page1 = new Page(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(setmealPageQueryDTO.getName() != null, Setmeal::getName, setmealPageQueryDTO.getName())
                .eq(setmealPageQueryDTO.getStatus() != null, Setmeal::getStatus, setmealPageQueryDTO.getStatus())
                .eq(setmealPageQueryDTO.getCategoryId() != null, Setmeal::getCategoryId, setmealPageQueryDTO.getCategoryId())
                .orderByDesc(Setmeal::getCreateTime);
        page1 = this.page(page1, queryWrapper);

        BeanUtils.copyProperties(page1, voPage, "records");
        LambdaQueryWrapper<Category> queryWrapper1 = new LambdaQueryWrapper<>();
        ArrayList<SetmealVO> setmealVOS = new ArrayList<>();
        for (Setmeal setmeal : page1.getRecords()) {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                setmealVO.setCategoryName(category.getName());
            }
            setmealVOS.add(setmealVO);
        }
        return new PageResult(this.count(), setmealVOS);
    }

    @Override
    @Transactional
    public void delSetmeal(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        this.remove(queryWrapper);

        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();

        queryWrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(queryWrapper1);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getId, id);

        Category category = categoryService.getById(setmeal.getCategoryId());
        setmealVO.setCategoryName(category.getName());
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Setmeal::getId, setmealDTO.getId());
        this.update(setmeal, lambdaUpdateWrapper);

        LambdaUpdateWrapper<SetmealDish> lambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper1.eq(SetmealDish::getSetmealId, setmealDTO.getId());
        setmealDishService.remove(lambdaUpdateWrapper1);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmeal1 : setmealDishes) {
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setName(setmeal1.getName());
            setmealDish.setDishId(setmeal1.getDishId());
            setmealDish.setCopies(setmeal1.getCopies());
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDish.setPrice(setmeal1.getPrice());
            setmealDishService.save(setmealDish);
        }

    }

    @Override
    public List<DishItemVO> getDish(Long id) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        List<DishItemVO> dishItemVOList=new ArrayList<>();

        for (SetmealDish setmealDish : list) {
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(setmealDish,dishItemVO);
            LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<Dish>();
            lambdaQueryWrapper.eq(Dish::getId, setmealDish.getDishId());
            Dish dish = dishService.getOne(lambdaQueryWrapper);
            dishItemVO.setDescription(dish.getDescription());
            dishItemVO.setImage(dish.getImage());
            dishItemVOList.add(dishItemVO);
        }
        return dishItemVOList;
    }

    @Override
    public SetmealOverViewVO queryOverviewSetmeals() {
        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Setmeal::getStatus, StatusConstant.ENABLE);
        Integer sold = (int) this.count(dishLambdaQueryWrapper);

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getStatus, StatusConstant.DISABLE);
        Integer discontinued = (int) this.count(lambdaQueryWrapper);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

}
